// Copyright 2012 Google Inc. All Rights Reserved.

package com.google.appengine.syncserver.processor;

import com.google.appengine.syncserver.entities.ClientIdGenerator;
import com.google.appengine.syncserver.shared.ClientIdRequest;
import com.google.appengine.syncserver.shared.ClientIdResponse;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;

import java.io.IOException;

/**
 * Creates a new client id and responds with id.
 */
public class ClientIdProcessor extends AbstractProcessor {
  /**
   * Gets {@link ClientIdResponse} object for corresponding {@link ClientIdRequest}.
   *
   * @param reqObject
   * @return ClientIdResponse object.
   */
  private static ClientIdResponse processClientIdRequest(ClientIdRequest reqObject) {
    String username = reqObject.getUsername();
    long clientId = ClientIdGenerator.getNextClientId(username);
    return new ClientIdResponse(clientId);
  }

  @Override
  public String processRequest(String reqStr)
      throws JsonParseException, JsonMappingException, IOException {
    ClientIdRequest request = ClientIdRequest.valueOf(reqStr);
    return processClientIdRequest(request).toString();
  }
}
